package day3.HomeWork;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

/*一级菜单
  1. 注册功能-->随机生成9个  150开头的手机号   供选择
  2. 登录功能-->登录成功进入二级菜单
  3. 退出系统-->退出程序
        二级菜单
  1. 展示用户信息-->编号(根据下标+1)  手机号  密码  姓名
  2. 删除用户信息-->根据编号 删除账号   本机无法删除
  3. 退出登录-->退出到一级菜单*/
public class UserSystem {
    static Scanner sc = new Scanner(System.in);
    static List<User> users = new ArrayList<>();
    static Random r = new Random();
    static int[] arr = new int[9];

    static {
        input();
    }

    public static void main(String[] args) {
        menu1();
    }

    private static void menu1() {
        while (true) {
            System.out.println("XXX用户系统");
            System.out.println("**********");
            System.out.println("1.注册");
            System.out.println("2.登录");
            System.out.println("3.退出");
            System.out.println("**********");
            int n = sc.nextInt();
            switch (n) {
                case 1:
                    register();
                    break;
                case 2:
                    login();
                    break;
                case 3:
                    return;
            }
        }
    }

    private static void menu2() {
        while (true) {
            System.out.println("1.查看用户信息");
            System.out.println("2.删除用户信息");
            System.out.println("3.退出登录");
            int n = sc.nextInt();
            switch (n) {
                case 1:
                    show();
                    break;
                case 2:
                    delete();
                    break;
                case 3:
                    return;
            }
        }
    }

    private static void register() {
        System.out.println("****注册****");
        System.out.println("请输入电话:");
        for (int i = 0; i < arr.length; i++) {
            arr[i] = r.nextInt(9000000) + 1000000;
            System.out.print("[" + "150" + arr[i] + "] ");
        }
        System.out.println();
        String phone = sc.next();
        if (isUserExists(phone)) {
            System.out.println("用户已存在!");
            return;
        }
        System.out.println("请输入密码:");
        String password = sc.next();
        System.out.println("请输入姓名:");
        String name = sc.next();
        users.add(new User(phone, password, name));
        System.out.println("注册成功!");
        output();
    }

    private static void login() {
        System.out.println("****登录****");
        System.out.println("请输入电话:");
        String phone = sc.next();
        System.out.println("请输入密码:");
        String password = sc.next();
        System.out.println("请输入姓名:");
        String name = sc.next();
        boolean b = false;
        if (isUserExists(phone)) {
            b = true;
            for (User user : users) {
                if (user.getPhone().equals(phone) && user.getPassword().equals(password) && user.getName().equals(name)) {
                    System.out.println("登录成功! 欢迎" + user.getName());
                    menu2();
                    return;
                }
            }
        }
        if (b) {
            System.out.println("登录失败!!");
        } else {
            System.out.println("用户不存在,请注册！");
        }
    }

    private static boolean isUserExists(String phone) {
        for (User user : users) {
            if (user.getPhone().equals(phone)) {
                return true;
            }
        }
        return false;
    }


    public static void delete() {
        System.out.println("请输入要删除的编号:");
        int n = sc.nextInt();
        users.remove(n - 1);
        System.out.println("删除成功!");
        output();
    }

    public static void show() {
        System.out.println("编号\t\t手机号\t密码\t姓名");
        for (int i = 0; i < users.size(); i++) {
            User u = users.get(i);
            System.out.println((i + 1) + "\t" + u.getPhone() + "\t" + u.getPassword() + "\t" + u.getName());
        }
    }

    //写入
    public static void output() {
        try (FileOutputStream fos = new FileOutputStream("d://a//user.xxx");
             ObjectOutputStream oos = new ObjectOutputStream(fos)) {
            oos.writeObject(users);
            System.out.println("集合更新成功!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //读取
    public static void input() {
        try (FileInputStream fis = new FileInputStream("d://a//user.xxx");
             ObjectInputStream ois = new ObjectInputStream(fis)) {
            users = (List<User>) ois.readObject();
            System.out.println("集合加载成功!");
        } catch (FileNotFoundException e) {
            System.out.println("欢迎第一次使用!");
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

}